package com.ba.base.view.dialog

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import com.ba.base.R
import com.ba.base.utils.WxShareUtils
import kotlinx.android.synthetic.main.dialog_shared.*
import kotlinx.coroutines.Job

/**
 * 分享底部弹框
 */
class SharedDialog : BaseDialog {

    var bitmap: Bitmap? = null
    var url: String? = null
    var title: String? = null
    var id: String? = null
    var isFlash = false

    constructor(context: Context) : super(context)

    override fun getContentViewResId(): Int {
        return R.layout.dialog_shared
    }

    override fun initView() {
        btn_cancel.setOnClickListener {
            dismiss()
        }
        bitmap = ContextCompat.getDrawable(context, R.drawable.ic_hat)?.toBitmap()
        iv_friend.setOnClickListener {
            if (WxShareUtils.shareWebToFriend(
                    context,
                    url,
                    title,
                    bitmap
                )
            ) {
//                shareCount()

            }
            dismiss()
        }
        iv_friends_circle.setOnClickListener {
            if (WxShareUtils.shareWebToFriendCircle(
                    context,
                    url,
                    title,
                    bitmap
                )
            ) {
//                shareCount()
            }
            dismiss()
        }
//        iv_tip_off.setOnClickListener {
//            ApiAuth.checkLogin(context, auth = false, jump = false) {
//                //举报
//                ARouter.getInstance().build("/mine/tip-off")
//                    .withString("title", "我的举报")
//                    .withString("url", "${CoreConfig.BASE_URL}/#/myCheck?token=${ApiAuth.getBearerBase()}&userId=${ApiAuth.userId}")
//                    .navigation()
//                dismiss()
//            }
//
//        }
    }

    override fun onSetContentViewAfter() {
        // 设置宽度为屏宽, 靠近屏幕底部。
        window?.run {
            // 一定要设置Background，如果不设置，window属性设置无效
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setGravity(Gravity.BOTTOM)
            val display = windowManager.defaultDisplay
            val lp = attributes
            display.getMetrics(DisplayMetrics())
            // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
            lp.width = ViewGroup.LayoutParams.MATCH_PARENT
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT
            attributes = lp
        }
        setCancelable(true)
    }

    private val mJob = Job()

//    private fun shareCount() {//只要拉起微信就算是分享成功
//        CoroutineScope(mJob).retrofit<BaseResult<Any?>> {
//            api = if(isFlash){
//                ApiEngine.createService(ApiFlash::class.java)
//                    .shareCount(id, ApiAuth.user?.nickName)
//            }else{
//                ApiEngine.createService(ApiManuscript::class.java)
//                    .shareCount(id, ApiAuth.user?.nickName)
//            }
//            onSuccess {
//
//            }
//        }
//    }
}
